<?php

namespace app\models\common;

use think\Model;

class OutApiAccessLogModel extends Model
{
    protected $connection = 'mongodb';
    protected $table = 'cm_outapi_access_log';


    /**
     * Note:添加记录
     * User:端木凌风
     * Date:2022-11-23 15:26
     */
    public function createRecord($oauthClientId, $sourceUrl, $data, array $param = [])
    {
        $data = [
            'oauth_client_id' => (string)$oauthClientId,
            'request_url' => (string)$sourceUrl,
            'data' => (string)$data,
            'create_time' => date('Y-m-d H:i:s'),
            'request_time' => (string)($param['request_time'] ?? date('Y-m-d H:i:s')),
            'response_data' => (string)($param['response_data'] ?? ''),
            'response_code' => (string)(strtolower($param['response_code'] ?? '')),
            'request_type' => (string)(strtolower($param['request_type']) ?? '')
        ];
        self::table($this->table)->insert($data);
        return true;
    }

    /**
     * Note:记录列表
     * @param $param
     * @param int $start
     * @param int $size
     * @return array
     */
    public function getRecordList(array $param = [], int $start = 0, int $size = 10)
    {
        $pdo = self::table($this->table);
        $pdo = $this->createSearchParam($pdo, $param);
        if ($size > 0) {
            $pdo->limit($start, $size);
        }
        $list = $pdo->order('request_time', 'desc')->select();
        $list = $list ? $list->toArray() : [];
        return $list;
    }

    /**
     * 获取记录总数
     * @param array $param
     * @return mixed
     * @date 2020-08-17 9:16
     */
    public function getRecordCount(array $param = [])
    {
        $pdo = self::table($this->table);
        $pdo = $this->createSearchParam($pdo, $param);
        return $pdo->count();
    }

    /**
     * 获取数据详细
     * @param $id
     * @return array
     * @date 2020-08-17 9:45
     */
    public function getInfoById($id, array $where = [])
    {
        $info = self::table($this->table)->where('_id', '=', $id)->find();
        $info = $info ? $info->toArray() : [];
        if ($info) {
            $info['data_format'] = $info['data'] ? json_encode(json_decode($info['data'], true), JSON_UNESCAPED_UNICODE) : '';
            $info['response_data_format'] = $info['response_data'] ? json_encode(json_decode($info['response_data'], true), JSON_UNESCAPED_UNICODE) : '';
        }
        return $info;
    }


    /**
     * Note:组装查询条件
     * User:端木凌风
     * @param $pdo
     * @param array $param
     * @return array
     */
    private function createSearchParam($pdo, array $param = [])
    {
        if (isset($param['request_type']) && $param['request_type']) {
            $pdo->where('request_type', '=', $param['request_type']);
        }
        if (isset($param['response_code']) && $param['response_code']) {
            $pdo->where('response_code', '=', $param['response_code']);
        }
        if (isset($param['oauth_client_id']) && (string)$param['oauth_client_id'] != '') {
            $pdo->where('oauth_client_id', '=', $param['oauth_client_id']);
        }
        return $pdo;
    }

}